package hhm.utils.client;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CookieStore;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;

import com.alibaba.fastjson.JSONObject;

public class Download {
	public static void main(String[] args) {
		File file = new File("D://342");
		InputStream in = null;
		OutputStream out = null;
		CloseableHttpClient httpClient = HttpClients.createDefault();

		CookieStore cookieStore = new BasicCookieStore();
		BasicClientCookie cookie = new BasicClientCookie("seid", "3435");
		cookieStore.addCookie(cookie);

		CredentialsProvider credsProvider = new BasicCredentialsProvider();
		credsProvider.setCredentials(new AuthScope("192.168.10.33", 80), new UsernamePasswordCredentials("admin", ""));

		HttpHost target = new HttpHost("192.168.10.33", 80, "http");
		HttpClientContext context = HttpClientContext.create();
		context.setCookieStore(cookieStore);

		context.setCredentialsProvider(credsProvider);

		HttpPost httpPost = new HttpPost("/config/icfg_conf_download");
		JSONObject json = new JSONObject();
		json.put("file_name", "running-config");
		json.put("file_name_radio", "running-config");
		json.put("sid", "");

		try {

			MultipartEntityBuilder mEntityBuilder = MultipartEntityBuilder.create();
			mEntityBuilder.addTextBody("file_name", "running-config");
			mEntityBuilder.addTextBody("file_name_radio", "running-config");
			List<NameValuePair> formParams = new ArrayList<NameValuePair>();
			formParams.add(new BasicNameValuePair("file_name", "running-config"));
			formParams.add(new BasicNameValuePair("file_name_radio", "running-config"));

			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");
			httpPost.setEntity(entity);
			HttpResponse response = httpClient.execute(target, httpPost, context);

			in = response.getEntity().getContent();
			out = new FileOutputStream(file);
			byte[] buffer = new byte[4096];
			int readLength = 0;
			while ((readLength = in.read(buffer)) > 0) {
				byte[] bytes = new byte[readLength];
				System.arraycopy(buffer, 0, bytes, 0, readLength);
				out.write(bytes);
			}

			out.flush();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
